package com.excerise.poly.array;

/**
 *
 *
 * @author Way on 2021/8/3
 */
public class PolyArray {

    public static void main(String[] args) {
        // 应用实例：现有一个继承结构如下：要求创建一个Person类
        // 2个Student 对象和2个Teacher对象，统一放在数组中，并调用每个对象的say方法
        Person[] persons = new Person[5];
        persons[0] = new Person("jack", 20);
        persons[1] = new Student("花木兰", 25, 33);
        persons[2] = new Student("安琪拉", 16, 33);
        persons[3] = new Teacher("赵云", 23, 3000);
        persons[4] = new Teacher("黄忠", 60, 3500);

        for (int i = 0; i < persons.length; i++) {
            System.out.println(persons[i].say());
            if (persons[i] instanceof Student) {
                ((Student) persons[i]).study();
            } else if (persons[i] instanceof Teacher) {
                ((Teacher) persons[i]).teach();
            }
        }
    }
}
